package com.etalk.crm.serviceImpl;

import com.etalk.crm.dao.StudentReportCustomizeMapper;
import com.etalk.crm.pojo.StudentReportCustomize;
import com.etalk.crm.service.StudentReportCustomizeService;
import com.etalk.crm.utils.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Jordan
 */
@Service
public class StudentReportCustomizeServiceImpl implements StudentReportCustomizeService {
    private static final Logger logger = LogManager.getLogger(StudentReportCustomizeServiceImpl.class);

    @Resource
    private StudentReportCustomizeMapper studentReportCustomizeMapper;

    /**
     * 生成自定义学习报表
     * @param personId 学员id
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param creator 结束时间
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public int createReportByCustomize(int personId,Date startDate,Date endDate,String creator) {
        logger.info("学员报表生成开始，开始时间[{}],结束时间[{}]",startDate,endDate);

        StudentReportCustomize studentReportCustomize = studentReportCustomizeMapper.selectPersonAndClassHourInfoByCustomize(personId,startDate, endDate);
        if (studentReportCustomize != null && studentReportCustomize.getPersonId()>0) {
            StudentReportCustomize mistakeStudentReportCustomize = studentReportCustomizeMapper.selectWrongQuestionInfoByCustomize(personId,startDate, endDate);
            StudentReportCustomize orderStudentReportCustomize = studentReportCustomizeMapper.selectOrderRemainHoursByCustomize(personId,startDate, endDate);
            StudentReportCustomize teacherStudentReportCustomize = studentReportCustomizeMapper.selectCommonTeacherInfoByCustomize(personId,startDate, endDate);
            //组合数据

            studentReportCustomize.setStartTime(startDate);
            studentReportCustomize.setEndTime(endDate);
            studentReportCustomize.setGrammarMistake(0);
            studentReportCustomize.setPronunciationMistake(0);
            studentReportCustomize.setWordMistake(0);
            studentReportCustomize.setRemainHours(BigDecimal.ZERO);
            studentReportCustomize.setTeacherImage("");
            studentReportCustomize.setTeacherLogin("");
            studentReportCustomize.setCreator(creator);
            if (org.apache.commons.lang3.StringUtils.isEmpty(studentReportCustomize.getNamedContentIds())){
                studentReportCustomize.setNamedContentIds("");
            }
            if (org.apache.commons.lang3.StringUtils.isEmpty(studentReportCustomize.getEvaluateLessonIds())){
                studentReportCustomize.setEvaluateLessonIds("");
            }
            //根据时间段计算建议上的课时，建议一周5节课
            int days= DateUtil.daysBetween(startDate,endDate);
            studentReportCustomize.setRecommendHoursThis(Math.round(days*5/7));
            if (StringUtils.isEmpty(studentReportCustomize.getSscLogin())) {
                studentReportCustomize.setSscLogin("");
            }
            if (StringUtils.isEmpty(studentReportCustomize.getTotalClass())) {
                studentReportCustomize.setTotalClass(0);
            }
            if (StringUtils.isEmpty(studentReportCustomize.getKnowledgeNum())) {
                studentReportCustomize.setKnowledgeNum(0);
            }
            if (StringUtils.isEmpty(studentReportCustomize.getKnowledgeError())) {
                studentReportCustomize.setKnowledgeError(0);
            }
            if (StringUtils.isEmpty(studentReportCustomize.getTotalComplete())){
                studentReportCustomize.setTotalComplete(0);
            }
            if (StringUtils.isEmpty(studentReportCustomize.getAttendanceRate())){
                studentReportCustomize.setAttendanceRate(0);
            }
            if (StringUtils.isEmpty(studentReportCustomize.getNamedTimes())){
                studentReportCustomize.setNamedTimes(0);
            }
            if (StringUtils.isEmpty(studentReportCustomize.getFourStarFeedback())){
                studentReportCustomize.setFourStarFeedback(0);
            }
            //计算打败百分之多少人
            studentReportCustomize.setDefeatedPeopleNum(BigDecimal.ZERO);
            if (StringUtils.isEmpty(studentReportCustomize.getStuCnName())) {
                studentReportCustomize.setStuCnName("");
            }

            if (mistakeStudentReportCustomize != null) {
                studentReportCustomize.setGrammarMistake(mistakeStudentReportCustomize.getGrammarMistake());
                studentReportCustomize.setPronunciationMistake(mistakeStudentReportCustomize.getPronunciationMistake());
                studentReportCustomize.setWordMistake(mistakeStudentReportCustomize.getWordMistake());
            }
            if (orderStudentReportCustomize != null) {
                studentReportCustomize.setRemainHours(orderStudentReportCustomize.getRemainHours().divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_HALF_UP));
            }
            if (teacherStudentReportCustomize != null) {
                studentReportCustomize.setTeacherImage(teacherStudentReportCustomize.getTeacherImage());
                studentReportCustomize.setTeacherLogin(teacherStudentReportCustomize.getTeacherLogin());
            }
            try {
                // 插入数据
                int num = studentReportCustomizeMapper.insertBatchReportList(studentReportCustomize);
                if (num > 0) {
                    logger.info("学员报表数据库月报信息，插入记录数：[{}]" ,num);
                }
                logger.info("学员报表生成结束，开始时间[{}],结束时间[{}]",startDate,endDate);
                return num;
            }catch (DuplicateKeyException e){
                logger.error(e);
                return -1;
            }
        }
        return 0;
    }
}
